package com.youkeda.comment.control;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.youkeda.comment.model.Comment;
import com.youkeda.comment.model.Paging;
import com.youkeda.comment.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CommentController
 * @Author 刘正星
 * @Date 2020/7/12 13:36
 **/
@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;

    @GetMapping("/comments")
    @ResponseBody
    public Paging<Comment> getAll(@RequestParam(value = "pageNum" ,required = false,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "15") Integer pageSize) {
        pageNum = pageNum == null ? 0:pageNum;
        Page<Comment> page = PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> commentService.findAll());
//        commentService.findAll().forEach(comment -> {
//            System.out.println(comment.getAuthor().getNickName()+" ==="+comment.getAuthor().getId());
//        });
        //page.getResult().stream().forEach(System.out::println);

        return new Paging<>(page.getPageNum(), page.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }

    @PostMapping("/comment")
    @ResponseBody
    public Comment save(@RequestBody Comment comment) {
        commentService.add(comment);
        return comment;
    }

    @PostMapping("/comment/update")
    @ResponseBody
    public Comment update(@RequestBody Comment comment) {
        commentService.update(comment);
        return comment;
    }

    @GetMapping("/comment/del")
    @ResponseBody
    public boolean delete(@RequestParam("id") long id) {
        return commentService.delete(id) > 0;
    }

    @GetMapping("/comment/findByRefId")
    @ResponseBody
    public List<Comment> findByRefId(@RequestParam("refId") String refId) {
        return commentService.findByRefId(refId);
    }

    @PostMapping("/comment/batchAdd")
    @ResponseBody
    public boolean batchAdd(@RequestParam("list") List<Comment> commentS) {
        return commentService.batchAdd(commentS) > 0;
    }

    @GetMapping("/comment/findByUserIds")
    @ResponseBody
    public List<Comment> findByUserIds(@RequestParam("userIds") List<Long> ids) {
        return commentService.findByUserIds(ids);
    }


}
